package top.lyjwn.todo.weinxin.data.mch.param;

import top.lyjwn.todo.common.utils.RandomValue;
import top.lyjwn.todo.common.utils.XMLUtil;

/**
 * 微信申请退款参数
 *
 * @author luoyujie
 * @link https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
 */
public class ApplyRefund implements XmlFormat {

    /**
     * 必填;
     * <p>
     * 微信分配的公众账号ID（企业号corpid即为此appId）
     */
    private String appid;
    /**
     * 必填;
     * <p>
     * 微信支付分配的商户号
     */
    private String mch_id;
    /**
     * 必填;
     * <p>
     * 随机字符串，不长于32位。推荐随机数生成算法
     */
    private String nonce_str;

    /**
     * 必填;
     * <p>
     * 签名，详见签名生成算法
     */
    private String sign;
    /**
     * 非必填;
     * <p>
     * 签名类型，目前支持HMAC-SHA256和MD5，默认为MD5
     */
    private String sign_type = "MD5";
    // =====================================二选一=======================================
    /**
     * 微信生成的订单号，在支付通知中有返回
     */
    private String transaction_id;

    /**
     * 商户系统内部订单号，要求32个字符内，只能是数字、大小写字母_-|*@ ，且在同一个商户号下唯一。
     */
    private String out_trade_no;
    // =====================================二选一=======================================

    /**
     * 必填;
     * <p>
     * 商户系统内部的退款单号，商户系统内部唯一，只能是数字、大小写字母_-|*@ ，同一退款单号多次请求只退一笔。
     */
    private String out_refund_no;

    /**
     * 必填;
     * <p>
     * 订单总金额，单位为分，只能为整数，详见支付金额
     */
    private Integer total_fee;
    /**
     * 必填;
     * <p>
     * 退款总金额，订单总金额，单位为分，只能为整数，详见支付金额
     */
    private Integer refund_fee;

    /**
     * 非必填;
     * <p>
     * 退款货币类型，需与支付一致，或者不填。符合ISO 4217标准的三位字母代码，默认人民币：CNY，其他值列表详见货币类型
     */
    private String refund_fee_type = "CNY";
    /**
     * 非必填;
     * <p>
     * 若商户传入，会在下发给用户的退款消息中体现退款原因 注意：若订单退款金额≤1元，且属于部分退款，则不会在退款消息中体现退款原因
     */
    private String refund_desc;

    /**
     * 非必填;
     * <p>
     * 仅针对老资金流商户使用
     * <p>
     * REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款（默认使用未结算资金退款）
     * REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款
     */
    private String refund_account = "REFUND_SOURCE_RECHARGE_FUNDS";

    /**
     * 非必填;
     * <p>
     * 异步接收微信支付退款结果通知的回调地址，通知URL必须为外网可访问的url，不允许带参数
     * <p>
     * 如果参数中传了notify_url，则商户平台上配置的回调地址将不会生效。
     */
    private String notify_url;

    public ApplyRefund() {
    }

    /**
     * @param out_trade_no 要退款的订单号，系统内部订单号
     * @param total_fee    订单金额
     * @param refund_fee   退款金额
     */
    public ApplyRefund(String appid,String mch_id,String out_trade_no, Integer total_fee, Integer refund_fee) {
        this.appid =appid;
        this.mch_id =mch_id;
        this.out_trade_no = out_trade_no;
        this.total_fee = total_fee;
        this.refund_fee = refund_fee;
        this.nonce_str = RandomValue.getRandomString(32);
        this.out_refund_no = "refund_" + out_trade_no;
    }

    /**
     * @param out_trade_no  要退款的订单号，系统内部订单号
     * @param total_fee     订单金额
     * @param refund_fee    退款金额
     * @param out_refund_no 退款订单号,可单独对应退款单
     */
    public ApplyRefund(String appid,String mch_id,String out_trade_no, Integer total_fee, Integer refund_fee, String out_refund_no) {
        this.appid =appid;
        this.mch_id =mch_id;
        this.out_trade_no = out_trade_no;
        this.total_fee = total_fee;
        this.refund_fee = refund_fee;
        this.nonce_str = RandomValue.getRandomString(32);
        this.out_refund_no = out_refund_no;
    }

//    public void generateSign() {
//        try {
//            this.sign = SignUtil.getSign(this);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    public ApplyRefund(String appid,String mch_id,String nonce_str, String sign, String sign_type, String transaction_id, String out_trade_no,
                       String out_refund_no, Integer total_fee, Integer refund_fee, String refund_fee_type, String refund_desc,
                       String refund_account, String notify_url) {
        this.appid =appid;
        this.mch_id =mch_id;
        this.nonce_str = nonce_str;
        this.sign = sign;
        this.sign_type = sign_type;
        this.transaction_id = transaction_id;
        this.out_trade_no = out_trade_no;
        this.out_refund_no = out_refund_no;
        this.total_fee = total_fee;
        this.refund_fee = refund_fee;
        this.refund_fee_type = refund_fee_type;
        this.refund_desc = refund_desc;
        this.refund_account = refund_account;
        this.notify_url = notify_url;
    }

    public String getNonce_str() {
        return nonce_str;
    }

    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getSign_type() {
        return sign_type;
    }

    public void setSign_type(String sign_type) {
        this.sign_type = sign_type;
    }

    public String getTransaction_id() {
        return transaction_id;
    }

    public void setTransaction_id(String transaction_id) {
        this.transaction_id = transaction_id;
    }

    public String getOut_trade_no() {
        return out_trade_no;
    }

    public void setOut_trade_no(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }

    public String getOut_refund_no() {
        return out_refund_no;
    }

    public void setOut_refund_no(String out_refund_no) {
        this.out_refund_no = out_refund_no;
    }

    public Integer getTotal_fee() {
        return total_fee;
    }

    public void setTotal_fee(Integer total_fee) {
        this.total_fee = total_fee;
    }

    public Integer getRefund_fee() {
        return refund_fee;
    }

    public void setRefund_fee(Integer refund_fee) {
        this.refund_fee = refund_fee;
    }

    public String getRefund_fee_type() {
        return refund_fee_type;
    }

    public void setRefund_fee_type(String refund_fee_type) {
        this.refund_fee_type = refund_fee_type;
    }

    public String getRefund_desc() {
        return refund_desc;
    }

    public void setRefund_desc(String refund_desc) {
        this.refund_desc = refund_desc;
    }

    public String getRefund_account() {
        return refund_account;
    }

    public void setRefund_account(String refund_account) {
        this.refund_account = refund_account;
    }

    public String getNotify_url() {
        return notify_url;
    }

    public void setNotify_url(String notify_url) {
        this.notify_url = notify_url;
    }

    public String getAppid() {
        return appid;
    }

    public String getMch_id() {
        return mch_id;
    }

    @Override
    public String toXml() {
        return XMLUtil.convertToXml(this);
    }


}
